﻿//------------------------------------------------------------------------------
//  
//------------------------------------------------------------------------------
#region Imported Namespaces
using System;
using System.Data;
using System.Configuration;
using System.Collections;

using SSRS;
using PHSRAG.Insight21.Reports;
using PHSRAG.Insight21.InsightUtilities;
using Utils = PHSRAG.Insight21.InsightUtilities;
using PHSRAG.Insight21.SearchContext;
//using UserContext = PHSRAG.Insight21.Context.UserContext;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;

#endregion

namespace PHSRAG.Insight21.Agreements.Reports {

    
    public class sqlBudgetBreakdownReport : sqlReport
    {
        private const string PageCacheKey = "BudgetBreakdown_00381C01-54D8-4846-9A91-CABFE38B39B7";
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_00381C01-54D8-4846-9A91-CABFE38B39B7";
		
        public sqlBudgetBreakdownReport(string pageUrl)
            : base(pageUrl)
        {
            doHashtable = true;      
        }
        
        public override string ReportName {
            get {
                return "Agmt_BudgetBreakdown";
            }
           
        }

        public override Hashtable setParameterHTable(ref string rptName)
        {
            reportTitle = "Budget Breakdown";
            rptName = ReportName;

            BudgetBreakdowns budgetBreakdowns = BudgetBreakdowns;

            reportSource = budgetBreakdowns.GetDataContainer();

            reportCriteria = loadCriteria(searchCriteria);

            return base.setParameterHTable(ref rptName);
        }

        protected override string loadCriteria(SearchCriteria searchCriteria)
        {
            DateTime GLAsofDate = (DateTime)((DataSet)cachePolicy.GetCachedObject(RefreshDatesControlKey)).Tables[0].Rows[0]["glPostAsOfDate"];
            return string.Format("Last GL Post|{0};", GLAsofDate.ToShortDateString() );
        }

        private BudgetBreakdowns BudgetBreakdowns
        {
            get
            {
               int searchVersion = searchCriteria.Version;

               BudgetBreakdowns breakdowns =
               Policy.ResultsCache.GetObject(cachePolicy, BudgetBreakdowns.CacheKey) as BudgetBreakdowns;
                if ((breakdowns == null) || (searchVersion != SearchCriteriaVersion))
                {
                    if (null != breakdowns)
                    {
                        Policy.ResultsCache.PurgeObject(cachePolicy);
                    }
                    breakdowns = new BudgetBreakdowns();

                    SearchCriteria clonedSearchCriteria = searchCriteria.Clone();

                    clonedSearchCriteria[Constants.GLPostAsOfDateKey] = GLPostAsOfDate.ToString();
                    clonedSearchCriteria[Constants.CategoryIDKey] = CategoryID.ToString();
                    clonedSearchCriteria[Constants.AccountNumberKey] = AccountNumber;
                    if (DetailsFlag)
                        clonedSearchCriteria[Constants.FundIDKey] = FundNumber;
                    clonedSearchCriteria[Constants.TransactionStartDateKey] = (BudgetStartDate != string.Empty) ? BudgetStartDate : null;
                    clonedSearchCriteria[Constants.TransactionEndDateKey] = (BudgetEndDate != string.Empty) ? BudgetEndDate : null;
                    clonedSearchCriteria[Constants.DetailsKey] = DetailsFlag.ToString();
                    breakdowns.Search(Session.SessionID, clonedSearchCriteria);

                    Policy.ResultsCache.SetObject(cachePolicy, BudgetBreakdowns.CacheKey, breakdowns);

                    SearchCriteriaVersion = searchVersion;
                }

                return breakdowns;
            }
        }

        private string AccountNumber
		{
			get
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				return (h == null || h[Keys.AccountNumberKey] == null) ? null : (string)h[Keys.AccountNumberKey];
			}
			set
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if (h == null)
					h = new Hashtable();
				h[Keys.AccountNumberKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}

        private int CategoryID
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
                if ((h == null) || (h[Keys.CategoryIDKey] == null))
                    throw new Exception("Budget breakdown page requires the Category ID to be specified");
                return (int)h[Keys.CategoryIDKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[Keys.CategoryIDKey] = value;
                pageCachePolicy.Cache(PageCacheKey, h);
            }
        }

        private bool DetailsFlag
		{
			get
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if ((h == null) || (h[Keys.DetailsKey] == null))
					throw new Exception("Budget breakdown page requires the DetailsFlag to be specified");
				return (bool)h[Keys.DetailsKey];
			}
			set
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if (h == null)
					h = new Hashtable();
				h[Keys.DetailsKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}

        public string FundNumber
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
                return (h == null || h[Keys.FundNumberKey] == null) ? null : (string)h[Keys.FundNumberKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[Keys.FundNumberKey] = (value == string.Empty) ? null : value;
                pageCachePolicy.Cache(PageCacheKey, h);
            }
        }

        public DateTime GLPostAsOfDate
		{
			get
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
				if ((h == null) || (h[Constants.GLPostAsOfDateKey] == null))
					throw new Exception("Budget breakdown page requires the GLPostAsOfDate to be specified");
				return (DateTime)h[Constants.GLPostAsOfDateKey];
			}
			set
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if (h == null)
					h = new Hashtable();
				h[Constants.GLPostAsOfDateKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}

		/// <summary>
		/// Get/Set the budget start date date which the breakdowns are loaded.
		/// </summary>
		public string BudgetStartDate
		{
			get
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
				return (h == null || h[Keys.BudgetStartDateKey] == null) ? string.Empty : (string)h[Keys.BudgetStartDateKey];
			}
			set
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if (h == null)
					h = new Hashtable();
				h[Keys.BudgetStartDateKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}

		/// <summary>
		/// Get/Set the budget end date date which the breakdowns are loaded.
		/// </summary>
		public string BudgetEndDate
		{
			get
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
				return (h == null || h[Keys.BudgetEndDateKey] == null) ? string.Empty : (string)h[Keys.BudgetEndDateKey];
			}
			set
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				if (h == null)
					h = new Hashtable();
				h[Keys.BudgetEndDateKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}

		/// <summary>
		/// Gets/sets an integer value indicating the search criteria version. 
		/// The version number changes when the search criteria changes.  This signals the page to re-build its
		/// cache.
		/// </summary>
		/// <remarks>
		/// This and All properties required to re-establish the page are stored as name / value pairs in
		/// a hash table stored in the session cache under the key Keys.FinancialSummaryPageKey.  
		/// This enables the page to re-establish its state with minimal IO to what it was when
		/// the user last visited it.
		/// </remarks>
		private int SearchCriteriaVersion
		{
			get 
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable; 
				return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int) h[SearchCriteriaVersionKey]; 
			}
			set 
			{
				Hashtable h = pageCachePolicy.GetCachedObject(PageCacheKey) as Hashtable;
				if (h == null) 
					h = new Hashtable();
				h[SearchCriteriaVersionKey] = value;
				pageCachePolicy.Cache(PageCacheKey, h);
			}
		}
	

 
    }
}
